package com.gyy.XmlTrans;

import com.gyy.sdk.HCNetSDK;

/**
 * 热成像透传
 *
 * @Author sdk支持
 * @Date 2021/4/1 17:54
 */
public class TransparentTransmission {
    static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;

    /**
     * 配置热成像原始数据为 全屏测温数据
     *
     * @param lUserID 用户登录句柄
     */
    public void configureStreamData(int lUserID) {

        HCNetSDK.NET_DVR_XML_CONFIG_INPUT inputParam = new HCNetSDK.NET_DVR_XML_CONFIG_INPUT();
        inputParam.dwSize = inputParam.size();

        HCNetSDK.BYTE_ARRAY requestUrl = new HCNetSDK.BYTE_ARRAY(256);
        String urlSting = "PUT /ISAPI/Thermal/channels/2/streamParam";
        System.arraycopy(urlSting.getBytes(), 0, requestUrl.byValue, 0, urlSting.length());
        requestUrl.write();
        inputParam.lpRequestUrl = requestUrl.getPointer();
        inputParam.dwRequestUrlLen = requestUrl.byValue.length;
        inputParam.dwRecvTimeOut = 5000;
        HCNetSDK.BYTE_ARRAY requestXml = new HCNetSDK.BYTE_ARRAY(16 * 1024);
        String xmlString =
                "<ThermalStreamParam version=\"2.0\"\n" +
                        "xmlns=\"http://www.hikvision.com/ver20/XMLSchema\">\n" +
                        "<videoCodingType>pixel-to-pixel_thermometry_data</videoCodingType>\n" +
                        "</ThermalStreamParam>";
        System.arraycopy(xmlString.getBytes(), 0, requestXml.byValue, 0, xmlString.length());
        requestXml.write();
        inputParam.lpInBuffer = requestXml.getPointer();
        inputParam.dwInBufferSize = requestXml.byValue.length;

        HCNetSDK.BYTE_ARRAY outputXml = new HCNetSDK.BYTE_ARRAY(16 * 1024);
        outputXml.read();
        HCNetSDK.NET_DVR_XML_CONFIG_OUTPUT outputParam = new HCNetSDK.NET_DVR_XML_CONFIG_OUTPUT();
        outputParam.dwSize = outputParam.size();
        outputParam.lpOutBuffer = outputXml.getPointer();
        outputParam.dwOutBufferSize = outputXml.byValue.length;

        HCNetSDK.BYTE_ARRAY setStatusBuff = new HCNetSDK.BYTE_ARRAY(16 * 1024);
        setStatusBuff.read();
        outputParam.lpStatusBuffer = setStatusBuff.getPointer();
        outputParam.dwStatusSize = setStatusBuff.byValue.length;
        outputParam.write();

        if (hCNetSDK.NET_DVR_STDXMLConfig(lUserID, inputParam, outputParam)) {
            System.out.println("透传成功");
            // TODO: 获取outputParam中返回数据
        }else {
            System.out.println("透传失败！！！ 错误码：" + hCNetSDK.NET_DVR_GetLastError());
        }
    }

    /**
     * 点击测温
     *
     * @param lUserID 用户登录句柄
     * @param positionX 点坐标x值
     * @param positionY 点坐标y值
     */
    public void clickGetTemperature(int lUserID , double positionX ,double positionY){
        HCNetSDK.NET_DVR_XML_CONFIG_INPUT inputParam = new HCNetSDK.NET_DVR_XML_CONFIG_INPUT();
        inputParam.dwSize = inputParam.size();

        HCNetSDK.BYTE_ARRAY requestUrl = new HCNetSDK.BYTE_ARRAY(256);
        String urlSting = "PUT /ISAPI/Thermal/channels/2/clickToThermometry/rules/1?format=json";
        System.arraycopy(urlSting.getBytes(), 0, requestUrl.byValue, 0, urlSting.length());
        requestUrl.write();
        inputParam.lpRequestUrl = requestUrl.getPointer();
        inputParam.dwRequestUrlLen = requestUrl.byValue.length;
        inputParam.dwRecvTimeOut = 5000;
        HCNetSDK.BYTE_ARRAY requestXml = new HCNetSDK.BYTE_ARRAY(16 * 1024);
        String jsonString =
                "{ " +
                    "\"ClickToThermometryRule\": {" +
                        "\"Point\": {" +
                            "\"positionX\" :" + positionX + "," +
                            "\"positionY\" :" + positionY +
                        " }" +
                    "}" +
                "}";
        System.arraycopy(jsonString.getBytes(), 0, requestXml.byValue, 0, jsonString.length());
        requestXml.write();
        inputParam.lpInBuffer = requestXml.getPointer();
        inputParam.dwInBufferSize = requestXml.byValue.length;

        HCNetSDK.BYTE_ARRAY outputXml = new HCNetSDK.BYTE_ARRAY(16 * 1024);
        outputXml.read();
        HCNetSDK.NET_DVR_XML_CONFIG_OUTPUT outputParam = new HCNetSDK.NET_DVR_XML_CONFIG_OUTPUT();
        outputParam.dwSize = outputParam.size();
        outputParam.lpOutBuffer = outputXml.getPointer();
        outputParam.dwOutBufferSize = outputXml.byValue.length;

        HCNetSDK.BYTE_ARRAY setStatusBuff = new HCNetSDK.BYTE_ARRAY(16 * 1024);
        setStatusBuff.read();
        outputParam.lpStatusBuffer = setStatusBuff.getPointer();
        outputParam.dwStatusSize = setStatusBuff.byValue.length;
        outputParam.write();

        if (hCNetSDK.NET_DVR_STDXMLConfig(lUserID, inputParam, outputParam)) {
            System.out.println("透传成功");
            // TODO: 获取outputParam中返回数据
        }else {
            System.out.println("透传失败！！！ 错误码：" + hCNetSDK.NET_DVR_GetLastError());
        }
    }
}
